// pages/visitingCardProduct/visitingCardProduct.js
const app = getApp()
const ctx = wx.createCanvasContext('visitingCardCanvas')
function canvasTextAutoLine(str,canvas,initX,initY,lineHeight){
    // var ctx = canvas.getContext("2d"); 
    var ctx = canvas; 
    var lineWidth = 5;
    var canvasWidth = 7; 
    var lastSubStrIndex= 0; 
    var initY2=initY+lineHeight;
    for(let i=0;i<str.length;i++){ 
        lineWidth+=ctx.measureText(str[i]).width; 
        if(i>11){
            ctx.fillText(str.substring(lastSubStrIndex,i),initX,initY2);
            initY+=lineHeight;
            lineWidth=0;
            lastSubStrIndex=i;
        } 
        if(i==str.length-1){
            ctx.fillText(str.substring(lastSubStrIndex,i+1),initX,initY);
        }
    }
  }
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:[],
    touimg:'',
    winheight:0,
    flag:0,
    photo:['https://tanghuzhao.cn/Data/UploadFiles/logo/bj1.jpg',
    'https://tanghuzhao.cn/Data/UploadFiles/logo/bj2.jpg',
    'https://tanghuzhao.cn/Data/UploadFiles/logo/bj3.jpg',
    'https://tanghuzhao.cn/Data/UploadFiles/logo/bj4.jpg'
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.imgs)
    var that = this
    that.setData({
        winheight: wx.getSystemInfoSync().windowWidth*1.17
    })
    var _this = this;
    var a=wx.getStorageSync("tempuser");
    var b=wx.getStorageSync("step");
    var c=wx.getStorageSync("stepk");
      console.log(a)
      if(a.address==0){
         that.setData({
             address:'',
         })
      }else{
       that.setData({
             address:a.address,
         })
      }
      if(a.phone==0){
         that.setData({
             phone:'',
         })
      }else{
       that.setData({
             phone:a.phone,
         })
      }
      if(a.weixin==0){
         that.setData({
             weixin:'',
         })
      }else{
       that.setData({
             weixin:a.name,
         })
      }
      if(a.email==0){
         that.setData({
             email:'',
         })
      }else{
       that.setData({
             email:a.email,
         })
      }
      if(a.company==0){
         that.setData({
             company:'',
         })
      }else{
       that.setData({
             company:a.company,
         })
      }
      if(a.username==0){
         that.setData({
             username:'',
         })
      }else{
       that.setData({
             username:a.username,
         })
      }
        that.setData({
           erma:a.erweima,
           name:a.nickname,
           userIcon:a.tou,
           step:b,
           stepk:c
        })
        if(app.globalData.imgs){
            that.setData({
                 posterimg:app.globalData.imgs
            })
            that.drawPic();
        }else{
          var photos=that.data.photo
            wx.downloadFile({
            url: photos[0],
            success(res) {
              // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
              if (res.statusCode === 200) {
                  that.setData({
                       posterimg:res.tempFilePath
                   })
                  console.log(photos[0])
                  that.drawPic();
              }
            }
         })
        }
     

  },
  /**
   * 绘制视图
   */
  drawPic(){
    var that = this
    var obj = that.data
    console.log(obj)
    let screenWidth = wx.getSystemInfoSync().windowWidth

      ctx.drawImage('/images/model.png', 0, 0, screenWidth, screenWidth * 1.17)
      ctx.save()
      ctx.beginPath()
      ctx.arc(60, 340, 30, 0, 2 * Math.PI)
      ctx.closePath()
      ctx.clip()
      ctx.drawImage(obj.userIcon, 30, 310, 60, 60)
      ctx.restore()
      if(obj.posterimg!=''){
      ctx.drawImage(obj.posterimg, 0, 0, screenWidth, screenWidth * 0.62)
      }
      //步数
      ctx.setTextAlign('left')
      ctx.setFillStyle("white")
      ctx.setStrokeStyle('white')
      ctx.setFontSize(28)
      ctx.fillText(obj.step+' 步', 20, 160)
      ctx.setFontSize(18)
      ctx.fillText('今日步数', 20, 210)
      ctx.drawImage('/images/bait.png', 130, 110, 2, 126)
      ctx.setFontSize(28)
      ctx.fillText(obj.stepk+'卡', 140, 160)
      ctx.setFontSize(18)
      ctx.fillText('燃烧我的卡路里', 140, 210)
      //二层
      ctx.setTextAlign('left')
      ctx.setFillStyle("#424E75")
      ctx.setStrokeStyle('#424E75')
      ctx.setFontSize(15)
      ctx.fillText(obj.company, 10, 290)

      ctx.setFontSize(18)
      ctx.setTextAlign('right')
      ctx.fillText(obj.username, 80, 400)

      ctx.drawImage('/images/heit.png', 110, 322, 2, 80)

      ctx.setTextAlign('left')
      ctx.setFillStyle("#424E75")
      ctx.setStrokeStyle('#424E75')
      ctx.setFontSize(13)
      ctx.fillText('手机:' + obj.phone, 120, 330)
      ctx.setFontSize(13)
      ctx.fillText('微信:' + obj.weixin, 120, 350)
      ctx.setFontSize(13)
      ctx.fillText('邮箱:' + obj.email, 120, 370)
      if(obj.address.length>11){
        var str=obj.address;
        var bb=str.substring(0,11)
        var aa=str.substring(11)
        console.log(bb)
        console.log(aa)
            
        ctx.setFontSize(13)
        ctx.fillText('地址:'+bb, 120, 390)
        ctx.setFontSize(13)
        ctx.fillText(aa, 120, 410)
      }else{
        ctx.fillText('地址:'+obj.address, 120, 390)
      }

      ctx.drawImage(obj.erma, screenWidth - 80, 360, 60, 60)

      ctx.draw()
  },

  /**
   * 保存名片
   */
  saveVisitingCard(){
    let screenWidth = wx.getSystemInfoSync().windowWidth
    wx.canvasToTempFilePath({
      x: 0,
      y: 0,
      width: screenWidth,
      height: screenWidth * 1.17,
      destWidth: screenWidth * 2,
      destHeight: screenWidth * 1.17 * 2,
      canvasId: 'visitingCardCanvas',
      success(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
        })
        wx.hideLoading()
        wx.showToast({
          title: '保存成功！',
          icon: 'success'
        })
        // setTimeout(function () {
        //   wx.navigateBack({
        //   })
        // }, 1000)
      }
    })
  },
  bindChooseType(e) {//选择相机还是相册
    var demo=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera', 'album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var imgs = app.globalData.imgs;
        // console.log(imgs)
        wx.navigateTo({
          url: "/pages/visitingCardProduct/upload?src=" + tempFilePaths
        });
        demo.setData({
          imgs: imgs
        });
      }
    });
  },
  modifyPoster:function(){
      var that=this;
      var photos=that.data.photo
      if(that.data.flag<3){
        var num=that.data.flag+1
          that.setData({
              flag:num
          })
      }else{
       that.setData({
              flag:0
       })
      }
       wx.downloadFile({
            url: photos[that.data.flag],
            success(res) {
              // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
              if (res.statusCode === 200) {
                  that.setData({
                       posterimg:res.tempFilePath
                   })
                  that.drawPic();
              }
            }
         })

       console.log(photos[that.data.flag])
       
      console.log(that.data.flag)
  },
  modify_user:function(){
     wx.navigateTo({
         url: '/pages/visitingCardSet/visitingCardSet', 
     })
  },
    /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})

